package com.knowif.backend.service;

import com.alibaba.fastjson.JSONObject;
import com.knowif.backend.dto.*;
import com.knowif.pojo.*;
import com.knowif.search.SearchCondition;
import com.knowif.service.*;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OfferInterviewService {

    @Autowired
    Offer_service offerService;
    @Autowired
    InterviewExperience_service interviewExperienceService;
    @Autowired
    User_service userService;
    @Autowired
    StanderSorce_service standerSorceService;
    @Autowired
    OfferInterviewComment_service offerInterviewCommentService;
    @Autowired
    OfferInterviewThanksCollection_service offerInterviewThanksCollectionService;

    public RestBean indexInfo(){
        try {
            OfferExample offerExample = new OfferExample();
            offerExample.setOrderByClause("create_time desc");
            List<Offer> offers = offerService.selectOffer(offerExample);
            List<OfferDto> offerDtos = buildOfferInfo(offers);
            InterviewExperienceExample interviewExperienceExample = new InterviewExperienceExample();
            interviewExperienceExample.setOrderByClause("create_time desc");
            List<InterviewExperience> interviewExperiences =
                    interviewExperienceService.selectInterviewExperience(interviewExperienceExample);
            List<InterviewExperienceDto> interviewExperienceDtos = buildInterviewInfo(interviewExperiences);

            JSONObject result = new JSONObject();
            result.put("offers", offerDtos);
            result.put("interviewExperiences", interviewExperienceDtos);
            return RestBean.buildSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestBean.buildError("参数错误");
    }

    /**
     * 模糊查询
     * @param key
     * @return
     */
    public RestBean<?> search(String key) {
        OfferExample offerExample = new OfferExample();
        offerExample.or(offerExample.createCriteria().andSchoolNameLike(key));
        offerExample.or(offerExample.createCriteria().andSpecialNameLike(key));
        offerExample.or(offerExample.createCriteria().andUserNameLike(key));
        List<Offer> offers = offerService.selectOffer(offerExample);
        List<OfferDto> offerDtos = buildOfferInfo(offers);

        InterviewExperienceExample interviewExperienceExample = new InterviewExperienceExample();
        interviewExperienceExample.or(interviewExperienceExample.createCriteria().andInterviewTitleLike(key));
        interviewExperienceExample.or(interviewExperienceExample.createCriteria().andSchoolNameLike(key));
        interviewExperienceExample.or(interviewExperienceExample.createCriteria().andInterviewSpecialLike(key));
        interviewExperienceExample.or(interviewExperienceExample.createCriteria().andUserNameLike(key));
        List<InterviewExperience> interviewExperiences = interviewExperienceService.selectInterviewExperience(interviewExperienceExample);
        List<InterviewExperienceDto> interviewExperienceDtos = buildInterviewInfo(interviewExperiences);

        JSONObject result = new JSONObject();
        result.put("offers", offerDtos);
        result.put("interviewExperiences", interviewExperienceDtos);
        return RestBean.buildSuccess(result);
    }

    public List<OfferDto> buildOfferInfo(List<Offer> offers){
        List<Integer> userIds = new ArrayList<>();
        List<OfferDto> offerDtos = new ArrayList<>();
        StanderSorceExample standerSorceExample = new StanderSorceExample();
        List<StanderSorce> standerSorces = standerSorceService.selectStanderSorce(standerSorceExample);
        if(!CollectionUtils.isEmpty(offers)){
            offers.forEach(offer -> {
                userIds.add(offer.getUserId());
                OfferDto offerDto = new OfferDto();
                offerDto.setOffer(offer);
                offerDtos.add(offerDto);
            });
            List<TUser> tUsers = userService.selectByUserIds(userIds);
            standerSorceExample.createCriteria().andUserIdIn(userIds);
            OfferDto.andTUser(offerDtos, tUsers);
            OfferDto.andStanderSorces(offerDtos, standerSorces);
        }
        return offerDtos;
    }

    public List<InterviewExperienceDto> buildInterviewInfo(List<InterviewExperience> interviewExperiences){
        List<InterviewExperienceDto> interviewExperienceDtos = new ArrayList<>();
        List<Integer> userIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(interviewExperiences)){
            interviewExperiences.forEach(interviewExperience -> {
                userIds.add(interviewExperience.getUserId());
                InterviewExperienceDto dto = new InterviewExperienceDto();
                dto.setInterviewExperience(interviewExperience);
                interviewExperienceDtos.add(dto);
            });
            List<TUser> tUsers = userService.selectByUserIds(userIds);
            InterviewExperienceDto.andTUser(interviewExperienceDtos, tUsers);
        }
        return interviewExperienceDtos;
    }

    public RestBean getInfo(Integer userId) {
        List<InfoCenter> infoCenter = this.offerInterviewCommentService.getInfoByUserId(userId);
        List<InfoCenterDto> infoCenterDtos = builderInfoCenterDtoList(infoCenter);
        return RestBean.buildSuccess(infoCenterDtos);
    }

    private List<InfoCenterDto> builderInfoCenterDtoList(List<InfoCenter> infoCenters){
        List<InfoCenterDto> infoCenterDtos = new ArrayList<>();
        List<Integer> userIds = new ArrayList<>();
        for(InfoCenter infoCenter : infoCenters){
            userIds.add(infoCenter.getCommentatorId());
            InfoCenterDto infoCenterDto = new InfoCenterDto();
            infoCenterDto.setInfoCenter(infoCenter);
            infoCenterDtos.add(infoCenterDto);
        }
        List<TUser> tUsers = userService.selectByUserIds(userIds);
        InfoCenterDto.addTUser(infoCenterDtos, tUsers);
        return infoCenterDtos;
    }

    public Map<String, Object> selectOfferInterviewComment(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        //根据userId查询offer
        OfferExample offerExample = new OfferExample();
        offerExample.createCriteria().andUserIdEqualTo(userId);
        List<Offer> offers = offerService.selectOffer(offerExample);
        List<OfferCountDto> offerCountDtos = builderOfferCountDto(offers);
        map.put("offers", offerCountDtos);
        //根据userId查询standerSorce
        StanderSorceExample standerSorceExample = new StanderSorceExample();
        standerSorceExample.createCriteria()
                .andUserIdEqualTo(userId);
        List<StanderSorce> standerSorces = standerSorceService.selectStanderSorce(standerSorceExample);
        map.put("standerSorce", standerSorces);
        //根据userId查询面经
        InterviewExperienceExample interviewExperienceExample = new InterviewExperienceExample();
        interviewExperienceExample.createCriteria()
                .andUserIdEqualTo(userId);
        List<InterviewExperience> interviewExperiences = interviewExperienceService.selectInterviewExperience(interviewExperienceExample);
        List<InterviewExperienceCountDto> interviewExperienceCountDtos = builderInterviewExperiences(interviewExperiences);
        map.put("interviews", interviewExperienceCountDtos);
        return map;
    }

    private List<InterviewExperienceCountDto> builderInterviewExperiences(List<InterviewExperience> interviewExperiences){
        ArrayList<InterviewExperienceCountDto> interviewExperienceCountDtos = new ArrayList<>();
        for(InterviewExperience interviewExperience : interviewExperiences){
            InterviewExperienceCountDto interviewExperienceCountDto = new InterviewExperienceCountDto();
            interviewExperienceCountDto.setInterviewExperience(interviewExperience);
            //统计评论数量
            OfferInterviewCommentExample offerInterviewCommentExample = new OfferInterviewCommentExample();
            offerInterviewCommentExample.createCriteria()
                    .andOfferInterviewIdEqualTo(interviewExperience.getId())
                    .andCommentTypeEqualTo(2);
            int commentCount = offerInterviewCommentService.countOfferInterviewCommentByParams(offerInterviewCommentExample);
            interviewExperienceCountDto.setCommentCount(commentCount);
            //统计感谢
            OfferInterviewThanksCollectionExample offerInterviewThanksCollectionExample = new OfferInterviewThanksCollectionExample();
            offerInterviewThanksCollectionExample.createCriteria()
                    .andOfferInterviewIdEqualTo(interviewExperience.getId())
                    .andCollectionTypeEqualTo(Byte.valueOf("2"))
                    .andThanksCollectionTypeEqualTo(1);
            int thanksCount = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(offerInterviewThanksCollectionExample);
            interviewExperienceCountDto.setThanksCount(thanksCount);
            //统计收藏
            OfferInterviewThanksCollectionExample offerInterviewThanksCollectionExample2 = new OfferInterviewThanksCollectionExample();
            offerInterviewThanksCollectionExample2.createCriteria()
                    .andOfferInterviewIdEqualTo(interviewExperience.getId())
                    .andCollectionTypeEqualTo(Byte.valueOf("2"))
                    .andThanksCollectionTypeEqualTo(2);
            int collectCount = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(offerInterviewThanksCollectionExample2);
            interviewExperienceCountDto.setCollectCount(collectCount);
            interviewExperienceCountDtos.add(interviewExperienceCountDto);
        }
        return interviewExperienceCountDtos;
    }

    private List<OfferCountDto> builderOfferCountDto(List<Offer> offers){
        ArrayList<OfferCountDto> offerCountDtos = new ArrayList<>();
        for(Offer offer : offers){
            OfferCountDto offerCountDto = new OfferCountDto();
            offerCountDto.setOffer(offer);
            //统计评论数量
            OfferInterviewCommentExample offerInterviewCommentExample = new OfferInterviewCommentExample();
            offerInterviewCommentExample.createCriteria()
                    .andOfferInterviewIdEqualTo(offer.getId())
                    .andCommentTypeEqualTo(1);
            int commentCount = offerInterviewCommentService.countOfferInterviewCommentByParams(offerInterviewCommentExample);
            offerCountDto.setCommentCount(commentCount);
            //统计感谢
            OfferInterviewThanksCollectionExample offerInterviewThanksCollectionExample = new OfferInterviewThanksCollectionExample();
            offerInterviewThanksCollectionExample.createCriteria()
                    .andOfferInterviewIdEqualTo(offer.getId())
                    .andCollectionTypeEqualTo(Byte.valueOf("1"))
                    .andThanksCollectionTypeEqualTo(1);
            int thanksCount = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(offerInterviewThanksCollectionExample);
            offerCountDto.setThanksCount(thanksCount);
            //统计收藏
            OfferInterviewThanksCollectionExample offerInterviewThanksCollectionExample2 = new OfferInterviewThanksCollectionExample();
            offerInterviewThanksCollectionExample2.createCriteria()
                    .andOfferInterviewIdEqualTo(offer.getId())
                    .andCollectionTypeEqualTo(Byte.valueOf("1"))
                    .andThanksCollectionTypeEqualTo(2);
            int collectCount = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(offerInterviewThanksCollectionExample2);
            offerCountDto.setCollectCount(collectCount);
            offerCountDtos.add(offerCountDto);
        }
        return offerCountDtos;
    }
}
